package br.eng.eliseu.gwt.gestaoWeb.client.ui.component.messageBox;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class MessageBoxPanel extends Composite {

	private static MessageBoxPanelUiBinder uiBinder = GWT.create(MessageBoxPanelUiBinder.class);
	interface MessageBoxPanelUiBinder extends UiBinder<Widget, MessageBoxPanel> {}

	@UiField DisclosurePanel messagePanel;
	@UiField Label headerText;
	@UiField FlexTable tabelaFTable;

	
	private List<String> listaMsg = new ArrayList<String>();
//	private Label headerText = new Label();
//	private DisclosurePanelHeader headerOpen;
	
	public MessageBoxPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		
//		messagePanel = new DisclosurePanel(open, open, "Teste");
		
		headerText.setText("Mensagens (0)");
//		Widget ht = messagePanel.getHeader();
//		ht.setTitle("Mensagens(0)");
//		messagePanel.setHeader(headerText);
//		messagePanel.setAnimationEnabled(true);
		this.setVisible(false);
		messagePanel.setOpen(false);
		
		setStyle(false);
		
	}
	
	public void setStyle( Boolean styleErro){
		if (styleErro){
			messagePanel.setStyleName("mensagemErroCabecalhoPanel");
			messagePanel.setStyleName("mensagemErroCorpoPanel");
		} else {
			messagePanel.setStyleName("mensagemOkCabecalhoPanel");
			messagePanel.setStyleName("mensagemOkCorpoPanel");
		}
	}
	/**
	 * Mostra as mensagens que estao armazenadas na lista de mensagens
	 * 
	 * @param 
	 * styleErro true indica uma mensagem vermelha de erro, false uma mensagem em verde
	 * @param open 
	 * true indica que as mensagens serao exibidas e nao so o titulo
	 */
	public void showMsg(Boolean styleErro, Boolean open){
		setStyle(styleErro);
		messagePanel.setOpen(open);
		populaMsgPanel();
	}
	public void addMsg( String msg){
		listaMsg.add(msg);
	}
	public void populaMsgPanel(){
		tabelaFTable.removeAllRows();
		int i=0;
		for (String s : listaMsg) {
			tabelaFTable.setHTML(i, 0, " ");
			tabelaFTable.setHTML(i, 1, s);
			i++;
		}
		headerText.setText("Mensagens ("+listaMsg.size()+")");
//		messagePanel.setHeader(headerText);
//		Widget ht = messagePanel.getHeader();
//		ht.setTitle("Mensagens ("+listaMsg.size()+")");
//		headerText.setText("Mensagens(0)");
//		messagePanel.setHeader(headerText);
		if (i>0){
			this.setVisible(true);
		} else {
			this.setVisible(false);
		}
	}
	
	public void clear(){
		listaMsg.clear();
	}
	
	
}


